/**
 * @(#)Server.java, Sep 7, 2012. Copyright 2012 Yodao, Inc. All rights reserved.
 *                  YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license
 *                  terms.
 */
package com.buaaer.mina;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.buaaer.mina.demo.DemoClass;
import com.buaaer.mina.demo.DemoInterface;

/**
 * @author wangheng
 */
public class Server {
    
    private int port;
    
    private Object instance;
    
    public Server(int port, Object instance) {
        this.port = port;
        this.instance = instance;
    }

    public void init() throws IOException {
        IoAcceptor ioAcceptor = new NioSocketAcceptor();
        ioAcceptor.getFilterChain().addLast("codec",
                new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        
        ServerHandler serverHandler = new ServerHandler(this.instance);
        ioAcceptor.setHandler(serverHandler);
        
        ioAcceptor.bind(new InetSocketAddress(this.port));
        
        System.out.println("Server start on " + this.port);
    }
    
}

